package binary.tests;

import binary.Binary16;
import binary.Binary64;
import junit.framework.TestCase;

public class Binary16Test extends TestCase {
	
	private void println(String S) {
		System.out.println(S);
	}
	
	private void printTitle(String S) {
		println("************************************************");
		println("***");
		println("*** " + S);
		println("***");
		println("************************************************");
	}
		
	@SuppressWarnings("unused")
	private void printItem(short B) {
		printItem("",B);
	}
	private void printItem(String S, short B) {
		println(S + Binary16.toBinaryString(B));
	}
	
	@SuppressWarnings("unused")
	private void printPair(short B1, short B2) {
		printPair("",B1,"",B2);
	}
	
	@SuppressWarnings("unused")
	private void printPair(String S1, short B1, short B2) {
		printPair(S1,B1,"",B2);
	}
	
	@SuppressWarnings("unused")
	private void printPair(short B1, String S2, short B2) {
		printPair("",B1,S2,B2);
	}
	
	private void printPair(String S1, short B1, String S2, short B2) {
		println(S1 + Binary16.toBinaryString(B1) + " : " + S2 + Binary16.toBinaryString(B2));
	}
	
	@SuppressWarnings("unused")
	private void printArray(short B[]) {
		printArray("",B);
	}
	
	private void printArray(String S, short B[]) {
		println(S + Binary16.toBinaryString(B));
	}
	
	@SuppressWarnings("unused")
	private void printArrays(short B1[], short B2[]) {
		printArrays("",B1,"",B2);
	}
	
	@SuppressWarnings("unused")
	private void printArrays(String S1, short B1[], short B2[]) {
		printArrays(S1,B1,"",B2);
	}
	
	@SuppressWarnings("unused")
	private void printArrays(short B1[], String S2, short B2[]) {
		printArrays("",B1,S2,B2);
	}
	
	
	private void printArrays(String S1, short B1[], String S2, short B2[]) {
		int		i;
		
		for(i=0; i<Math.max(B1.length, B2.length); i++) {
			S1 = String.format("Item[%3d] ==> ",i) + S1;
			printPair(S2, B1[i], S2, B2[i]);
		}
	}
	
	private short[] testValues() {
		short	B[] = new short[255];
		int		i;
		
		for(i=0; i<B.length; i++) {
			B[i] = (short) i; 
		}
		return B;
	}
	
	private short[] testKeyValues() {
		short	B[] = new short[] {
				Binary16.TRUE,
				Binary16.FALSE,
				Binary16.LEFT,
				Binary16.RIGHT,
				Binary16.FIRST,
				Binary16.LAST
		};
		
		return B;
	}
	
	private short[] testAllValue(short V) {
		short	B[] = new short[255];
		Binary16.setAll(V, B);
		return B;
	}
	
	private boolean compareArray(short A[], short B[]) {
		if (A.length == B.length) {
			int	i;
			for(i=0; i<A.length; i++) {
				if (A[i] != B[i]) {
					return false;
				}
			}
			return true;
		} else {
			return false;
		}
	}
	
	public final void testPrimitives() {
		printTitle("testPrimitives");
		short	B1 = (short) 0x55;
		
		boolean	R1;
		boolean	R2;
		int		i;
		
		printItem("B1    = ",B1);
		printItem("TRUE  = ",Binary16.TRUE);
		printItem("FALSE = ",Binary16.FALSE);
		printItem("FIRST = ",Binary16.FIRST);
		printItem("LAST  = ",Binary16.LAST);
		printItem("LEFT  = ",Binary16.LEFT);
		printItem("RIGHT = ",Binary16.RIGHT);
		
		
		
		for(i=0; i<Binary16.NUMBITS; i++) {
			R1	= Binary16.getBitFromRight(Binary16.NUMBITS-1-i, B1);
			R2	= Binary16.getBitFromLeft(i, B1);

			System.out.printf("R[%3d] == %5b     R[%3d] == %5b]\n", i, R1, i, R2);
			assertTrue(R1 == R2);
		}
	}
	
	public final void testReverseshort() {
		printTitle("testReverseshort");
		
		short	B1 = Binary16.LEFT;
		short	B2 = Binary16.reverse(B1);
		short	B3 = Binary16.reverse(B2);
		
		printItem("B1 = ", B1);
		printItem("B2 = ", B2);
		printItem("B3 = ", B3);
		
		assertTrue(B2 == Binary16.RIGHT);
		assertTrue(B3 == B1);
	}

	public final void testNotshort() {
		printTitle("testNotshort");

		short	B1 = Binary16.FALSE;
		short	B2 = Binary16.not(B1);
		short	B3 = Binary16.not(B2);
		
		printItem("B1 = ", B1);
		printItem("B2 = ", B2);
		printItem("B3 = ", B3);

		B2 = (short) (B1 ^ B2);
		assertTrue(B2 == Binary16.TRUE);
		assertTrue(B3 == B1);
	}

	public final void testGetBitFromLeftIntshort() {
		printTitle("testGetBitFromLeftIntshort");
		printItem("FIRST = ", Binary16.FIRST);
		
		assertTrue(Binary16.getBitFromLeft(0, Binary16.FIRST));
		assertFalse(Binary16.getBitFromLeft(1, Binary16.FIRST));
		assertFalse(Binary16.getBitFromLeft(2, Binary16.FIRST));
		assertFalse(Binary16.getBitFromLeft(3, Binary16.FIRST));
		assertFalse(Binary16.getBitFromLeft(4, Binary16.FIRST));
		assertFalse(Binary16.getBitFromLeft(5, Binary16.FIRST));
		assertFalse(Binary16.getBitFromLeft(6, Binary16.FIRST));
		assertFalse(Binary16.getBitFromLeft(7, Binary16.FIRST));
	}

	public final void testGetBitFromRightIntshort() {
		printTitle("testGetBitFromRightIntshort");

		assertFalse(Binary16.getBitFromRight(0, Binary16.FIRST));
		assertFalse(Binary16.getBitFromRight(1, Binary16.FIRST));
		assertFalse(Binary16.getBitFromRight(2, Binary16.FIRST));
		assertFalse(Binary16.getBitFromRight(3, Binary16.FIRST));
		assertFalse(Binary16.getBitFromRight(4, Binary16.FIRST));
		assertFalse(Binary16.getBitFromRight(5, Binary16.FIRST));
		assertFalse(Binary16.getBitFromRight(6, Binary16.FIRST));
		assertFalse(Binary16.getBitFromRight(7, Binary16.FIRST));
		assertFalse(Binary16.getBitFromRight(8, Binary16.FIRST));
		assertFalse(Binary16.getBitFromRight(9, Binary16.FIRST));
		assertFalse(Binary16.getBitFromRight(10, Binary16.FIRST));
		assertFalse(Binary16.getBitFromRight(11, Binary16.FIRST));
		assertFalse(Binary16.getBitFromRight(12, Binary16.FIRST));
		assertFalse(Binary16.getBitFromRight(13, Binary16.FIRST));
		assertFalse(Binary16.getBitFromRight(14, Binary16.FIRST));
		assertTrue(Binary16.getBitFromRight(15, Binary16.FIRST));
	}

	public final void testSetBitFromLeftIntBooleanshort() {
		printTitle("testSetBitFromLeftIntBooleanshort");
		short	B1 = Binary16.setBitFromLeft(0, true, Binary16.FALSE);
		short	B2 = Binary16.setBitFromLeft(Binary16.NUMBITS-1, true, Binary16.FALSE);

		printItem("B1 = ", B1);
		printItem("B2 = ", B2);

		assertTrue(B1 == Binary16.FIRST);
		assertTrue(B2 == Binary16.LAST);
	}

	public final void testSetBitFromRightIntBooleanshort() {
		printTitle("testSetBitFromRightIntBooleanshort");
		short	B1 = Binary16.setBitFromRight(0, true, Binary16.FALSE);
		short	B2 = Binary16.setBitFromRight(Binary16.NUMBITS-1, true, Binary16.FALSE);

		printItem("B1 = ", B1);
		printItem("B2 = ", B2);
		
		assertTrue(B1 == Binary16.LAST);
		assertTrue(B2 == Binary16.FIRST);
	}

	public final void testGetAllFromLeft() {
		printTitle("testGetAllFromLeft");
		short	B1	= Binary16.getAllFromLeft(Binary16.NUMBITS/2, Binary16.TRUE);
		short	B2	= Binary16.getAllFromLeft(Binary16.NUMBITS, Binary16.TRUE);
		short	B3	= Binary16.getAllFromLeft(1, Binary16.TRUE);
		short	B4	= Binary16.getAllFromLeft(0, Binary16.TRUE);
		
		printItem("B1 = ", B1);
		printItem("B2 = ", B2);
		printItem("B3 = ", B3);
		printItem("B4 = ", B4);
		
		assertTrue(B1 == Binary16.LEFT);
		assertTrue(B2 == Binary16.TRUE);
		assertTrue(B3 == Binary16.FIRST);
		assertTrue(B4 == Binary16.FALSE);

	}

	public final void testGetAllFromRight() {
		printTitle("testGetAllFromRight");
		short	B1	= Binary16.getAllFromRight(Binary16.NUMBITS/2, Binary16.TRUE);
		short	B2	= Binary16.getAllFromRight(Binary16.NUMBITS, Binary16.TRUE);
		short	B3	= Binary16.getAllFromRight(1, Binary16.TRUE);
		short	B4	= Binary16.getAllFromRight(0, Binary16.TRUE);
		
		printItem("B1 = ", B1);
		printItem("B2 = ", B2);
		printItem("B3 = ", B3);
		printItem("B4 = ", B4);
		
		assertTrue(B1 == Binary16.RIGHT);
		assertTrue(B2 == Binary16.TRUE);
		assertTrue(B3 == Binary16.LAST);
		assertTrue(B4 == Binary16.FALSE);
	}

	public final void testSetAllFromLeft() {
		printTitle("testSetAllFromLeft");
		short	B1 = Binary16.FALSE;
		short	B2 = Binary16.setAllFromLeft(Binary16.NUMBITS/2, true, B1);
		short	B3 = Binary16.setAllFromLeft(Binary16.NUMBITS/2, false, B2);
		short	B4 = Binary16.setAllFromLeft(Binary16.NUMBITS, true, B1);
		
		printItem("B1 = ", B1);
		printItem("B2 = ", B2);
		printItem("B3 = ", B3);
		printItem("B4 = ", B4);
		
		assertTrue(B2 == Binary16.LEFT);
		assertTrue(B3 == Binary16.FALSE);
		assertTrue(B4 == Binary16.TRUE);
	}

	public final void testSetAllFromRight() {
		printTitle("testSetAllFromRight");
		short	B1 = Binary16.FALSE;
		short	B2 = Binary16.setAllFromRight(Binary16.NUMBITS/2, true, B1);
		short	B3 = Binary16.setAllFromRight(Binary16.NUMBITS/2, false, B2);
		short	B4 = Binary16.setAllFromRight(Binary16.NUMBITS, true, B1);
		
		printItem("B1 = ", B1);
		printItem("B2 = ", B2);
		printItem("B3 = ", B3);
		printItem("B4 = ", B4);

		assertTrue(B2 == Binary16.RIGHT);
		assertTrue(B3 == Binary16.FALSE);
		assertTrue(B4 == Binary16.TRUE);
	}

	public final void testGetBitsLeftToRight() {
		printTitle("testGetBitsLeftToRight");
		
		short	B1 = Binary16.getBitsLeftToRight(0, Binary16.NUMBITS/2, Binary16.TRUE);
		short	B2 = Binary16.getBitsLeftToRight(Binary16.NUMBITS/2, Binary16.NUMBITS/2, Binary16.TRUE);
		short	B3 = Binary16.getBitsLeftToRight(0, 1, Binary16.TRUE);
		short	B4 = Binary16.getBitsLeftToRight(Binary16.NUMBITS-1, 1, Binary16.TRUE);
		short	B5 = Binary16.getBitsLeftToRight(0, 0, Binary16.TRUE);
		short	B6 = Binary16.getBitsLeftToRight(0, Binary16.NUMBITS, Binary16.TRUE);

		printItem("B1 = ", B1);
		printItem("B2 = ", B2);
		printItem("B3 = ", B3);
		printItem("B4 = ", B4);
		printItem("B5 = ", B5);
		printItem("B6 = ", B6);
		
		assertTrue(B1 == Binary16.LEFT);
		assertTrue(B2 == Binary16.RIGHT);
		assertTrue(B3 == Binary16.FIRST);
		assertTrue(B4 == Binary16.LAST);
		assertTrue(B5 == Binary16.FALSE);
		assertTrue(B6 == Binary16.TRUE);
	}

	public final void testGetBitsRightToLeft() {
		printTitle("testGetBitsRightToLeft");
		
		short	B1 = Binary16.getBitsRightToLeft(0, Binary16.NUMBITS/2, Binary16.TRUE);
		short	B2 = Binary16.getBitsRightToLeft(Binary16.NUMBITS/2, Binary16.NUMBITS/2, Binary16.TRUE);
		short	B3 = Binary16.getBitsRightToLeft(0, 1, Binary16.TRUE);
		short	B4 = Binary16.getBitsRightToLeft(Binary16.NUMBITS-1, 1, Binary16.TRUE);
		short	B5 = Binary16.getBitsRightToLeft(0, 0, Binary16.TRUE);
		short	B6 = Binary16.getBitsRightToLeft(0, Binary16.NUMBITS, Binary16.TRUE);

		printItem("B1 = ", B1);
		printItem("B2 = ", B2);
		printItem("B3 = ", B3);
		printItem("B4 = ", B4);
		printItem("B5 = ", B5);
		printItem("B6 = ", B6);
		
		assertTrue(B1 == Binary16.RIGHT);
		assertTrue(B2 == Binary16.LEFT);
		assertTrue(B3 == Binary16.LAST);
		assertTrue(B4 == Binary16.FIRST);
		assertTrue(B5 == Binary16.FALSE);
		assertTrue(B6 == Binary16.TRUE);	
	}

	public final void testCopy() {
		printTitle("testCopy");
		
		short	B[] = testValues();
		short	C[] = Binary16.copy(B);
		
		printArray("B = ", B);
		printArray("B = ", C);
		assertTrue(compareArray(B,C));
		
	}

	public final void testReverseshortArray() {
		printTitle("testReverseshortArray");
		
		short	B[];
		short	R1[];
		short	R2[];
		int		i;
		boolean	cmp;
		
		B = testValues();
		
		R1 = Binary16.copy(B);
		Binary16.reverse(R1);
		
		R2 = Binary16.copy(R1);
		Binary16.reverse(R2);
		
		for(i=0; i<B.length; i++) {
			cmp = (B[i] == R1[R1.length-1-i]);
			System.out.printf("B[%3d] vs R1[%3d] == %b\n", i, R1.length-1-i, cmp);
			assertTrue(cmp);
		}
		
		assertTrue(compareArray(B,R2));
	}

	public final void testReverseBits() {
		printTitle("testReverseBits");
		
		short	B[];
		short	R1[];
		short	R2[];
		int		i;
		boolean	cmp;
		
		B = testValues();
		
		R1 = Binary16.copy(B);
		Binary16.reverseBits(R1);
		
		R2 = Binary16.copy(R1);
		Binary16.reverseBits(R2);
		
		for(i=0; i<B.length; i++) {
			cmp = (B[i] == Binary16.reverse( R1[R1.length-1-i]) );
			System.out.printf("B[%3d] vs R1[%3d] == %b\n", i, R1.length-1-i, cmp);
			assertTrue(cmp);
		}
		
		assertTrue(compareArray(B,R2));

	}

	public final void testGetBitFromLeftIntshortArray() {
		printTitle("testGetBitFromLeftIntshortArray");
		short	B[] = testValues();
		int		i, j, k;
		boolean	V1, V2;
		
		k = 0;
		for(i=0; i<B.length; i++) {
			for(j=0; j<Binary16.NUMBITS; j++) {
				V1 = Binary16.getBitFromLeft(j, B[i]);
				V2 = Binary16.getBitFromLeft(k, B);
				System.out.printf("ArrayBit[%3d] : (Item[%3d], ItemBit[%3d]) == %5b : %5b\n", k, i, j, V1, V2);
				assertTrue(V1==V2);
				k++;
			}
		}
	}

	public final void testGetBitFromRightIntshortArray() {
		printTitle("testGetBitFromRightIntshortArray");
		short	B[] = testKeyValues();
		int		i, j, k;
		boolean	V1, V2;
		
		printArray("B = ", B);

		k = 0;
		for(i=B.length-1; i>=0; i--) {
			for(j=0; j<Binary16.NUMBITS; j++) {
				V1 = Binary16.getBitFromRight(j, B[i]);
				V2 = Binary16.getBitFromRight(k, B);
				System.out.printf("ArrayBit[%3d] : (Item[%3d], Item[%3d]) == %5b : %5b\n", k, i, j, V1, V2);
				assertTrue(V1==V2);
				k++;
			}
		}	
	}

	public final void testSetBitFromLeftIntBooleanshortArray() {
		printTitle("testSetBitFromLeftIntBooleanshortArray");
		short	B[] = testAllValue(Binary16.FALSE);
		int		i, j, k;
		
		k = 0;
		for(i=0; i<B.length; i++) {
			for(j=0; j<Binary16.NUMBITS; j++) {
				Binary16.setBitFromLeft(k, true, B);
				k++;
			}
		}
		
		for(i=0; i<B.length; i++) {
			String	S = String.format("Item[%d] = ", i);
			printItem(S,B[i]);
			assertTrue(B[i] == Binary16.TRUE);
		}
	}

	public final void testSetBitFromRightIntBooleanshortArray() {
		printTitle("testSetBitFromRightIntBooleanshortArray");
		short	B[] = testAllValue(Binary16.FALSE);
		int		i, j, k;
		
		k = 0;
		for(i=0; i<B.length; i++) {
			for(j=0; j<Binary16.NUMBITS; j++) {
				Binary16.setBitFromRight(k, true, B);
				k++;
			}
		}
		
		for(i=0; i<B.length; i++) {
			String	S = String.format("Item[%d] = ", i);
			printItem(S,B[i]);
			assertTrue(B[i] == Binary16.TRUE);
		}
	}

	public final void testSetAll() {
		printTitle("testSetAll");
		short	B[] = testValues();
		short	V;
		int		i;
		
		V = Binary16.FALSE;
		Binary16.setAll(V, B);
		for(i=0; i<B.length; i++) {
			assertTrue(B[i] == V);
		}
		
		V = Binary16.TRUE;
		Binary16.setAll(V, B);
		for(i=0; i<B.length; i++) {
			assertTrue(B[i] == V);
		}
	}

	public final void testSetAllBits() {
		printTitle("testSetAllBits");
		short	B[] = testValues();
		int		i;
		boolean	cmp;
		
		println("Setting values to anything but false");
		for(i=0; i<B.length; i++) {
			B[i] = Binary16.TRUE;
			cmp = (B[i] != Binary16.FALSE);
			System.out.printf("Checking B[%3d] != FALSE => %5b\n", i, cmp);
			assertTrue(cmp);
		}
		
		println("Setting all values to FALSE");
		Binary16.setAll(Binary16.FALSE, B);
		for(i=0; i<B.length; i++) {
			cmp = (B[i] == Binary16.FALSE);
			System.out.printf("Checking B[%3d] == FALSE => %5b\n", i, cmp);
			assertTrue(cmp);
		}
	}

	public final void testAnd() {
		printTitle("testAnd");
		short	B1[] = testAllValue(Binary16.RIGHT);
		short	B2[] = testAllValue(Binary16.LEFT);
		short	B3[] = Binary16.and(B1, B2);
		short	B4[] = Binary16.and(B1, B1);
		int		i;
		
		for(i=0; i<B3.length; i++) {
			String S = String.format("Item[%3d] = ", i);
			printItem(S, B3[i]);
			assertTrue(B3[i] == Binary16.FALSE);
		}
		
		for(i=0; i<B4.length; i++) {
			String S = String.format("Item[%3d] = ", i);
			printItem(S, B4[i]);
			assertTrue(B4[i] == Binary16.RIGHT);
		}
		
	}

	public final void testOr() {
		printTitle("testOr");
		short	B1[] = testAllValue(Binary16.RIGHT);
		short	B2[] = testAllValue(Binary16.LEFT);
		short	B3[] = Binary16.or(B1, B2);
		short	B4[] = Binary16.or(B1, B1);
		int		i;
		
		for(i=0; i<B3.length; i++) {
			String S = String.format("Item[%3d] = ", i);
			printItem(S, B3[i]);
			assertTrue(B3[i] == Binary16.TRUE);
		}
		
		for(i=0; i<B4.length; i++) {
			String S = String.format("Item[%3d] = ", i);
			printItem(S, B4[i]);
			assertTrue(B4[i] == Binary16.RIGHT);
		}
	}

	public final void testXor() {
		printTitle("testXor");
		short	B1[] = testAllValue(Binary16.RIGHT);
		short	B2[] = testAllValue(Binary16.LEFT);
		short	B3[] = Binary16.xor(B1, B2);
		short	B4[] = Binary16.xor(B1, B1);
		int		i;
		
		for(i=0; i<B3.length; i++) {
			String S = String.format("Item[%3d] = ", i);
			printItem(S, B3[i]);
			assertTrue(B3[i] == Binary16.TRUE);
		}
		
		for(i=0; i<B4.length; i++) {
			String S = String.format("Item[%3d] = ", i);
			printItem(S, B4[i]);
			assertTrue(B4[i] == Binary16.FALSE);
		}
	}

	public final void testNotshortArray() {
		printTitle("testNotshortArray");
		short	B1[] = testAllValue(Binary16.RIGHT);
		short	B2[] = testAllValue(Binary16.LEFT);
		short	B3[] = Binary16.not(B1);
		short	B4[] = Binary16.not(B2);
		int		i;
		
		for(i=0; i<B3.length; i++) {
			String S = String.format("Item[%3d] = ", i);
			printItem(S, B3[i]);
			assertTrue(B3[i] == Binary16.LEFT);
		}
		
		for(i=0; i<B4.length; i++) {
			String S = String.format("Item[%3d] = ", i);
			printItem(S, B4[i]);
			assertTrue(B4[i] == Binary16.RIGHT);
		}
	}

	public final void testShiftRight() {
		//
		// TODO: Make this more efficient so we are shifting more than one bit at a time
		//
		printTitle("testShiftLeft");
		
		short	B[] = testKeyValues();
		short	C[];
		int		i, numBits, shift;
		
		shift = 7;
		
		numBits = B.length*Binary16.NUMBITS;
		
		printArray("B == ", B);
		
		C = Binary16.shiftLeft(shift, B);
		printArray("C == ", C);
		
		for(i=0; i<numBits-shift; i++) {
			assertTrue(	Binary16.getBitFromLeft(i, C) ==	Binary16.getBitFromLeft(i + shift, B) );
		}
		for(i=numBits-shift; i<numBits; i++) {
			assertTrue(	Binary16.getBitFromLeft(i, C) ==	false );
		}
	}

	public final void testShiftLeft() {
		//
		// TODO: Make this more efficient so we are shifting more than one bit at a time
		//
		printTitle("testShiftLeft");
		
		short	B[] = testKeyValues();
		short	C[];
		int		i, numBits, shift;
		
		shift = 7;
		
		numBits = B.length*Binary16.NUMBITS;
		
		printArray("B == ", B);
		
		C = Binary16.shiftLeft(shift, B);
		printArray("C == ", C);
		
		for(i=0; i<numBits-shift; i++) {
			assertTrue(	Binary16.getBitFromLeft(i, C) ==	Binary16.getBitFromLeft(i + shift, B) );
		}
		for(i=numBits-shift; i<numBits; i++) {
			assertTrue(	Binary16.getBitFromLeft(i, C) ==	false );
		}
	}

	public final void testToBinaryStringshort() {
		printTitle("testToBinaryStringshort");
		
		String	tmp;

		tmp = Binary16.toBinaryString(Binary16.TRUE);
		println("tmp == " + tmp);
		assertTrue("1111111111111111".compareTo(tmp)==0);
		
		tmp = Binary16.toBinaryString(Binary16.FALSE);
		println("tmp == " + tmp);
		assertTrue("0000000000000000".compareTo(tmp)==0);
		
		tmp = Binary16.toBinaryString(Binary16.LEFT);
		println("tmp == " + tmp);
		assertTrue("1111111100000000".compareTo(tmp)==0);
		
		tmp = Binary16.toBinaryString(Binary16.RIGHT);
		println("tmp == " + tmp);
		assertTrue("0000000011111111".compareTo(tmp)==0);
		
		tmp = Binary16.toBinaryString(Binary16.FIRST);
		println("tmp == " + tmp);
		assertTrue("1000000000000000".compareTo(tmp)==0);
		
		tmp = Binary16.toBinaryString(Binary16.LAST);
		println("tmp == " + tmp);
		assertTrue("0000000000000001".compareTo(tmp)==0);
		
	}

	public final void testToHexStringshort() {
		printTitle("testToHexStringshort");
		
		String	tmp;

		tmp = Binary16.toHexString(Binary16.TRUE);
		println("tmp == " + tmp);
		assertTrue("FFFF".compareTo(tmp)==0);
		
		tmp = Binary16.toHexString(Binary16.FALSE);
		println("tmp == " + tmp);
		assertTrue("0000".compareTo(tmp)==0);
		
		tmp = Binary16.toHexString(Binary16.LEFT);
		println("tmp == " + tmp);
		assertTrue("FF00".compareTo(tmp)==0);
		
		tmp = Binary16.toHexString(Binary16.RIGHT);
		println("tmp == " + tmp);
		assertTrue("00FF".compareTo(tmp)==0);
		
		tmp = Binary16.toHexString(Binary16.FIRST);
		println("tmp == " + tmp);
		assertTrue("8000".compareTo(tmp)==0);
		
		tmp = Binary16.toHexString(Binary16.LAST);
		println("tmp == " + tmp);
		assertTrue("0001".compareTo(tmp)==0);
		
	}

	public final void testToQuadStringshort() {
		printTitle("testToQuadStringshort");
		
		String	tmp;

		tmp = Binary16.toQuadString(Binary16.TRUE);
		println("tmp == " + tmp);
		assertTrue("33333333".compareTo(tmp)==0);
		
		tmp = Binary16.toQuadString(Binary16.FALSE);
		println("tmp == " + tmp);
		assertTrue("00000000".compareTo(tmp)==0);
		
		tmp = Binary16.toQuadString(Binary16.LEFT);
		println("tmp == " + tmp);
		assertTrue("33330000".compareTo(tmp)==0);
		
		tmp = Binary16.toQuadString(Binary16.RIGHT);
		println("tmp == " + tmp);
		assertTrue("00003333".compareTo(tmp)==0);
		
		tmp = Binary16.toQuadString(Binary16.FIRST);
		println("tmp == " + tmp);
		assertTrue("20000000".compareTo(tmp)==0);
		
		tmp = Binary16.toQuadString(Binary16.LAST);
		println("tmp == " + tmp);
		assertTrue("00000001".compareTo(tmp)==0);
		
	}

	public final void testToOctalStringshort() {
		printTitle("testToOctalStringshort");
		// 0000000011111111
		// 00  0  3  7  7
		// 1000000000000000
		// 10  0  0  0  0
		String	tmp;

		tmp = Binary16.toOctalString(Binary16.TRUE);
		println("tmp == " + tmp);
		assertTrue("177777".compareTo(tmp)==0);
		
		tmp = Binary16.toOctalString(Binary16.FALSE);
		println("tmp == " + tmp);
		assertTrue("000000".compareTo(tmp)==0);
		
		tmp = Binary16.toOctalString(Binary16.LEFT);
		println("tmp == " + tmp);
		assertTrue("177400".compareTo(tmp)==0);
		
		tmp = Binary16.toOctalString(Binary16.RIGHT);
		println("tmp == " + tmp);
		assertTrue("000377".compareTo(tmp)==0);
		
		tmp = Binary16.toOctalString(Binary16.FIRST);
		println("tmp == " + tmp);
		assertTrue("100000".compareTo(tmp)==0);
		
		tmp = Binary16.toOctalString(Binary16.LAST);
		println("tmp == " + tmp);
		assertTrue("000001".compareTo(tmp)==0);
		
	}

	public final void testToBinaryStringshortArray() {
		printTitle("testToBinaryStringshortArray");
		short	B[] = testKeyValues();
		String	S = Binary16.toBinaryString(B);
		String	E = "\n1111111111111111\n0000000000000000\n1111111100000000\n0000000011111111\n1000000000000000\n0000000000000001\n";
		
		println("S == " + S);
		println("E == " + E);
		
		assertTrue(	S.equals(E) );
	}

	public final void testToHexStringshortArray() {
		printTitle("testToHexStringshortArray");
		short	B[] = testKeyValues();
		String	S = Binary16.toHexString(B);
		String	E = "\nFFFF\n0000\nFF00\n00FF\n8000\n0001\n";
		
		println("S == " + S);
		println("E == " + E);
		
		assertTrue(	S.equals(E) );
	}

	public final void testToOctalStringshortArray() {
		printTitle("testToOctalStringshortArray");
		// TODO: Fix this
		short	B[] = testKeyValues();
		String	S = Binary16.toOctalString(B);
		String	E = "\n177777\n000000\n177400\n000377\n100000\n000001\n";
		
		println("S == " + S);
		println("E == " + E);
		
		assertTrue(	S.equals(E) );
	}

	public final void testToArrayShortArray() {
		printTitle("testToArrayShortArray");
		short B[] = testKeyValues();
		int A[] = new int[] {
			(int) 0xFFFF0000,
			(int) 0xFF0000FF,
			(int) 0x80000001
		};
		short T[] = Binary16.toArray(A);
		
		assertTrue(compareArray(B,T));
	}

	public final void testToArrayIntArray() {
		printTitle("testToArrayIntArray");
		int A[] = new int[] {
			(int) 0x00000000,
			(int) 0xFFFF0000,
			(int) 0xFF0000FF,
			(int) 0x80000001
		};
		short B[] = new short[] {
				Binary16.FALSE,
				Binary16.FALSE,
				Binary16.TRUE,
				Binary16.FALSE,
				Binary16.LEFT,
				Binary16.RIGHT,
				Binary16.FIRST,
				Binary16.LAST
		};
		short T[] = Binary16.toArray(A);
		
		printArray("B = ", B);
		printArray("T = ", T);
		
		assertTrue(compareArray(B,T));
	}

	public final void testToArrayLongArray() {
		printTitle("testToArrayLongArray");
		long A[] = new long[] {
			(long) 0x00000000FFFF0000L,
			(long) 0xFF0000FF80000001L
		};
		short B[] = new short[] {
				Binary16.FALSE,
				Binary16.FALSE,
				Binary16.TRUE,
				Binary16.FALSE,
				Binary16.LEFT,
				Binary16.RIGHT,
				Binary16.FIRST,
				Binary16.LAST
		};
		short T[] = Binary16.toArray(A);
		int i;
		
		printArray("B = ", B);
		
		println("A = ");
		for(i=0;i<A.length;i++) {
			println(Binary64.toBinaryString(A[i]));
		}
		
		printArray("T = ", T);
		
		assertTrue(compareArray(B,T));
	}

}
